<template>
  <PageLayout full class="navigator">
    <template #header>
      <HeaderLayout @back="onBack">
        <view></view>
        <view></view>
        <view></view>
      </HeaderLayout>
    </template>
    <view class="loadingBox" hidden="{{loadStore.showAnnouncement}}">
      <view class="loadingBg"></view>
      <view class="loading">
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
        <view class="dot"></view>
      </view>
      <view class="progress">{{ $t('Navigator.Loading.Loading.Text') }}</view>
    </view>
    <Modal
      :ok-text="$t('Navigator.Loading.Acknowledge.Text')"
      :title="$t('Navigator.Loading.Hint.Text')"
      :visible="loadStore.errorHint"
      @ok="redirectToHome"
    >
      <text class="comError">{{ loadStore.errorTxt }}</text>
    </Modal>
    <Modal
      :title="$t('Navigator.Loading.Hint.Text')"
      :visible="loadStore.toOtherMiniprogram"
      :cancel-text="$t('Navigator.Loading.Cancel.Text')"
      :ok-text="loadStore.okText || $t('Navigator.Loading.Confirm.Text')"
      @ok="confirmJump"
      @cancel="cancelJump"
    >
      <view> {{ loadStore.customText || $t('Navigator.Loading.JumpMiniHint.Text') }}</view>
    </Modal>
  </PageLayout>
</template>
<script lang="ts">
export type announceItem = {
  effectTime?: string
  ineffectTime?: string
  content: string
}
export type announceRes = Array<announceItem>
</script>

<script setup lang="ts">
import PageLayout from '@/layout/page/page.vue'
import HeaderLayout from '@/layout/page/header.vue'
import Modal from '@/common/modal.vue'
import netconfig from '@/config/netconfig'
import { choosePath } from '@/utils/loading'
import { jumpMP } from '@/utils/mpHelper'
import { useLoadStore, type State } from '@/store/loading'
import { onLoad, onShow, onHide } from '@dcloudio/uni-app'
import { getAnnouncement, jumpBannerWeb } from '@/api/common'
import '@/styles/loading.less'

const loadStore = useLoadStore()

const assetsPre = netconfig.ASSETSPRE
let urlOptions = {
  miniprogram_id: '',
  appid: '',
  landingPage: '',
  path: '',
}
let init = false
let isAgain = false //是否为再次登录
let isSameDay = false //是否同一天

onLoad((options: any) => {
  if (options.landingPage) {
    // 解码options.landingPage
    options.landingPage = decodeURIComponent(options.landingPage)
  }
  urlOptions = options
  getAnnouncementList(0)
})
onShow(() => {
  if (isAgain === true) {
    loadStore.showAnnouncement = false
    loadStore.announcementContent = ''
    loadStore.announcementTime = ''
    isSameDay = false
    getAnnouncementList(1)
  }
  if (!init) {
    init = true
    return false
  }
  uni.redirectTo({
    url: '/pages/navigator/index/index',
  })
})
onHide(() => {
  isAgain = true
})
function onBack() {
  uni.reLaunch({
    url: '/pages/navigator/index/index?current=0',
  })
}
function redirectToHome() {
  loadStore.errorHint = false
  uni.redirectTo({
    url: '/pages/navigator/index/index',
  })
}

// 取消按钮，取消跳转本其他小程序
function cancelJump() {
  loadStore.toOtherMiniprogram = false
  uni.redirectTo({
    url: '/pages/navigator/index/index',
  })
}

// 确定按钮，跳转到其他小程序
async function confirmJump() {
  loadStore.toOtherMiniprogram = false
  let appid = urlOptions.miniprogram_id ? urlOptions.miniprogram_id : urlOptions.appid ? urlOptions.appid : ''
  let path = urlOptions.landingPage ? urlOptions.landingPage : urlOptions.path ? urlOptions.path : ''
  if (appid) {
    try {
      await jumpMP(appid, path, {}, 'Navigator.Loading.Confirm.Text')
    } catch {
      cancelJump()
    }
  } else if (loadStore.appId) {
    jumpBannerWeb(loadStore.bannerurl as string, `pages/navigator/index`, '2', loadStore.appId as string)
  } else {
    loadStore.errorHint = true
    loadStore.errorTxt = 'appid缺失'
  }
}

/**
 * 获取公告信息列表
 */
async function getAnnouncementList(isshowview: number) {
  await getAnnouncement().then(
    (res) => {
      if (res == null) {
        console.warn('index.js', 'getAnnouncementList', 'res is null')
        if (isshowview == 0) choosePath(urlOptions)
        return
      }
      let result = <announceRes>res.data
      if (result.length > 0) {
        if (
          result[0].effectTime != null &&
          result[0].effectTime.length > 0 &&
          result[0].ineffectTime != null &&
          result[0].ineffectTime.length > 0
        ) {
          var effectTime = result[0].effectTime
          var ineffectTime = result[0].ineffectTime
          effectTime = effectTime.replace(/-/g, '.')
          ineffectTime = ineffectTime.replace(/-/g, '.')
          var isSameDay = false
          if (effectTime.substring(0, 10) == ineffectTime.substring(0, 10)) {
            isSameDay = true
            effectTime = effectTime.substring(0, 16)
            ineffectTime = ineffectTime.substring(11, 16)
          } else {
            isSameDay = false
            effectTime = effectTime.substring(0, 16)
            ineffectTime = ineffectTime.substring(0, 16)
          }
          ;(loadStore.showAnnouncement = true),
            (loadStore.announcementContent = result[0].content),
            (loadStore.announcementTime = effectTime + '&nbsp;-&nbsp;' + ineffectTime),
            (isSameDay = isSameDay)
        } else {
          if (isshowview == 0) choosePath(urlOptions)
        }
      } else {
        if (isshowview == 0) choosePath(urlOptions)
      }
    },
    () => {
      if (isshowview == 0) {
        choosePath(urlOptions)
      }
    }
  )
}
</script>

<style lang="less">
.loadingBox {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
}
.loading {
  top: 50%;
  margin-top: -100rpx;
  transform: translate(0, -50%);
}
.progress {
  position: absolute;
  margin-top: -40rpx;
  top: 50%;
  width: 100%;
  font-size: 28rpx;
  color: #4475d5;
  text-align: center;
}
.comError {
  word-wrap: break-word;
  text-align: left;
  font-size: 30rpx;
  line-height: 48rpx;
  color: #131b35;
  display: block;
}
</style>
